package com.wwx.worldonwheels.service.impl;

import com.wwx.worldonwheels.VO.CarVO;
import com.wwx.worldonwheels.common.Constant;
import com.wwx.worldonwheels.common.result.CommonResult;
import com.wwx.worldonwheels.domain.*;
import com.wwx.worldonwheels.mapper.*;
import com.wwx.worldonwheels.service.CarReturnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * ClassName: CarReturnSerivceImpl
 * Description:
 *
 * @author HuaHX
 */
@Slf4j
@Transactional
@Service
public class CarReturnSerivceImpl implements CarReturnService {
    @Autowired
    private WwxServiceMapper serviceMapper;

    @Autowired
    private WwxVehicleMapper vehicleMapper;

    @Autowired
    private WwxInvoiceMapper invoiceMapper;

    @Autowired
    private WwxPaymentMapper paymentMapper;

    @Autowired
    private WwxDiscountMapper discountMapper;

    @Override
    public CommonResult ReturnCar(Integer serviceId,
                                  Integer endMile,
                                  String paymentMethod,
                                  String coupId,
                                  Integer cardNo) {

        log.info("The method‘s Entry is{}, {}, {}, {},{} ", serviceId, endMile, paymentMethod, coupId, cardNo);

        WwxService service = new WwxService();
        service.setServId(serviceId);
        service.setEndMeter(endMile);
        //update endMile
        serviceMapper.updateByPrimaryKeySelective(service);

        service = serviceMapper.selectByPrimaryKey(serviceId);
        String vin = service.getWwxVehicleVin();
        WwxVehicle vehicle = new WwxVehicle();
        vehicle.setVin(vin);
        vehicle.setUseStatus(Constant.VEHICLE_NOT_USE);
        //update car use status
        vehicleMapper.updateByPrimaryKeySelective(vehicle);

        CarVO car = vehicleMapper.findCar(vin);
        int daysDiff = (int) (service.getDropoffDate().getTime() - service.getPickupDate().getTime()) / (24 * 60 * 60 * 1000);
        Integer milageLimit = daysDiff * Constant.DEFAULT_ODOMETER_LIMIT;
        if(daysDiff==0){
            milageLimit=Constant.DEFAULT_ODOMETER_LIMIT;
        }

        //update milageLimit
        service.setMilageLimit(milageLimit);
        serviceMapper.updateByPrimaryKeySelective(service);

        //caculate money
        double amount = (double) Integer.parseInt(car.getDaily_rate()) * daysDiff;
        if(daysDiff==0){amount=(double)Integer.parseInt(car.getDaily_rate());}
        if (endMile > milageLimit) {
            amount += (endMile - milageLimit) * car.getMileage_rate();
        }
        if(!Constant.NO_COUPON.equals(coupId)){
            WwxDiscount discount =discountMapper.selectByPrimaryKey(coupId);
            if(discount==null){
                log.error("this code is invaild",coupId);
            }else{
                amount=amount*discount.getDisRate()/100;
            }

        }
        log.info("the deal,you should pay:{}$", amount);
        WwxInvoice invoice = new WwxInvoice();
        invoice.setInvoiceId(service.getWwxInvoiceInvoiceId());
        invoice.setAmount(BigDecimal.valueOf(amount));
        invoice.setDate(new Date());
        invoice.setStatus(Constant.ALREADY_PAID);
        invoiceMapper.updateByPrimaryKeySelective(invoice);

        //update payment
        WwxPayment payment = new WwxPayment();
        payment.setDate(new Date());
        payment.setAmount(BigDecimal.valueOf(amount));
        payment.setWwxInvoiceInvoiceId(invoice.getInvoiceId());
        payment.setCardNo(cardNo);
        payment.setMethod(paymentMethod);
        int res = paymentMapper.insert(payment);
        if (res == 0) {
            return CommonResult.failed(payment, "payment insert failed");
        }

        return CommonResult.success(invoice);

    }
}
